/*
*    Phantom Test Driver v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
/*
 * PTDRemoteFrame.java
 *
 * Created on July 21, 2008, 9:36 AM
 */

package ptd;

import java.util.Vector;
import javax.swing.ImageIcon;

/**
 *
 * @author  John
 */
public class PTDRemoteFrame extends javax.swing.JFrame {

    PTDTree SuiteTree; 
    protected AgentListModel myModel;
    
    /** Creates new form PTDRemoteFrame */
    public PTDRemoteFrame() {
        myModel = new AgentListModel();
        initComponents();
        ImageIcon a = PTDImageLoader.getImageIcon(PTDImageLoader.IMG_PTD_ICON);
        setIconImage(a.getImage());

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        RemoveButton = new javax.swing.JButton();
        StartButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();
        TreeScroll = new javax.swing.JScrollPane();
        jPanel1 = new javax.swing.JPanel();
        HostField = new javax.swing.JTextField();
        PortField = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        AddButton = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        AgentList = new javax.swing.JList();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("PTD Remote Execution");

        jLabel2.setText("Suite Items");

        jLabel3.setText("Remote Agents");

        RemoveButton.setMnemonic('D');
        RemoveButton.setText("Delete Selected Agents");
        RemoveButton.setToolTipText("Permanently removes selected agents from list");
        RemoveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                RemoveButtonActionPerformed(evt);
            }
        });

        StartButton.setMnemonic('S');
        StartButton.setText("Start");
        StartButton.setToolTipText("Start remote execution of selected suite in selected agents");
        StartButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                StartButtonActionPerformed(evt);
            }
        });

        CancelButton.setMnemonic('C');
        CancelButton.setText("Close");
        CancelButton.setToolTipText("Cancel without executing");
        CancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        HostField.setToolTipText("Network address of new Agent host");
        HostField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                HostFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                HostFieldFocusLost(evt);
            }
        });

        PortField.setToolTipText("Agent listening port on new Agent host");
        PortField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                PortFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                PortFieldFocusLost(evt);
            }
        });

        jLabel4.setText("Port");

        jLabel1.setText("Host");

        AddButton.setMnemonic('A');
        AddButton.setText("Add");
        AddButton.setToolTipText("Add new Agent to list");
        AddButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                AddButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(AddButton)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(HostField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 176, Short.MAX_VALUE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jLabel4)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(PortField, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 46, Short.MAX_VALUE)
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel4)
                    .add(PortField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(AddButton)
                    .add(HostField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        AgentList.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(AgentList);

        jScrollPane2.setViewportView(jScrollPane1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(TreeScroll, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 219, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                            .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                                .add(StartButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 75, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(CancelButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 71, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(jLabel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 355, Short.MAX_VALUE)
                            .add(RemoveButton))
                        .addContainerGap())
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 153, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 208, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(RemoveButton)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(32, 32, 32)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(StartButton)
                            .add(CancelButton)))
                    .add(TreeScroll, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 347, Short.MAX_VALUE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void CancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancelButtonActionPerformed
    dispose();
}//GEN-LAST:event_CancelButtonActionPerformed

private void StartButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_StartButtonActionPerformed
    int iSel[] = AgentList.getSelectedIndices();
    int iLen = iSel.length;
    
    if(iLen <= 0){
        PTDMessageBox.MessageBox("Must select at least one remote Agent.");
        return;
    }
    
    for(int i = 0; i < iLen; i++){
        if(iSel[i] >= myModel.Machines.size()){
            dispose();
            return;
        }
        SuiteTree.RunRemote(myModel.Machines.get(iSel[i]));
    }
    dispose();
}//GEN-LAST:event_StartButtonActionPerformed

private void AddButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_AddButtonActionPerformed
    PTDRemoteMachine M = new PTDRemoteMachine();
    String sHost = HostField.getText();
    String sPort = PortField.getText();
    if(sHost.length() <= 0){
        PTDMessageBox.MessageBox("Remote host address required.");
        return;
    }
    
    if(sPort.length() <= 0){
        PTDMessageBox.MessageBox("Remote port ID required.");
        return;
    }
    
    int iPort = 0;
    try{
        iPort = Integer.parseInt(sPort);
    }catch (Exception e){
        PTDMessageBox.MessageBox("Port ID must be an integer.");
        return;
    }
    
    M.myName = sHost;
    M.myPort = iPort;
    
    if(myModel == null){
        return;
    }
    
    myModel.addMachine(M);
}//GEN-LAST:event_AddButtonActionPerformed

private void RemoveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_RemoveButtonActionPerformed
    int iSel[] = AgentList.getSelectedIndices();
    int iLen = iSel.length;
    
    // Must be this way, else indices dont work out OK
    for(int i = iLen; i > 0; i--){
        myModel.removeMachine(iSel[i-1]);
    }
}//GEN-LAST:event_RemoveButtonActionPerformed

private void HostFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_HostFieldFocusGained
    getRootPane().setDefaultButton((AddButton));
}//GEN-LAST:event_HostFieldFocusGained

private void HostFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_HostFieldFocusLost
    getRootPane().setDefaultButton((StartButton));
}//GEN-LAST:event_HostFieldFocusLost

private void PortFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PortFieldFocusGained
    getRootPane().setDefaultButton((AddButton));
}//GEN-LAST:event_PortFieldFocusGained

private void PortFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_PortFieldFocusLost
    getRootPane().setDefaultButton((StartButton));
}//GEN-LAST:event_PortFieldFocusLost

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new PTDRemoteFrame().setVisible(true);
            }
        });
    }
    
    public void initialize(PTDTree In, PTDMain inPW){
        SuiteTree = new PTDTree(false);
        SuiteTree.setModel(In.getModel());
        SuiteTree.setParent(inPW);
        TreeScroll.setViewportView(SuiteTree);
        
        populateAgents(inPW.OpenWorkspace);
        
        getRootPane().setDefaultButton((StartButton));
    }
    
    protected void populateAgents(PTDWorkspace In){
        AgentList.removeAll();
        if(In == null){
            return;
        }
        
        if(In.Machines == null){
            return;
        }
        
        myModel.setMachines(In.Machines);
        AgentList.setModel(myModel);
    }
    
    protected class AgentListModel extends javax.swing.AbstractListModel {
        public Vector<PTDRemoteMachine> Machines = null;
        public int getSize() {
            if(Machines == null){
                return 0;
            }
            return Machines.size();
        }
        public Object getElementAt(int i) {
            if(Machines == null){
                return "";
            }
            String Ret;
            Ret = Machines.get(i).myName + " (Port=" + Integer.toString(Machines.get(i).myPort) + ")";
            return Ret;
        }
        public void setMachines(Vector<PTDRemoteMachine> inMachines){
            Machines = inMachines;
        };
        
        public void addMachine(PTDRemoteMachine Machine){
            Machines.add(Machine);
            this.fireIntervalAdded(this, Machines.size(), Machines.size());
        };

        public void removeMachine(int ind){
            Machines.remove(ind);
            this.fireIntervalRemoved(this, ind, ind);
        };

        
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AddButton;
    private javax.swing.JList AgentList;
    private javax.swing.JButton CancelButton;
    private javax.swing.JTextField HostField;
    private javax.swing.JTextField PortField;
    private javax.swing.JButton RemoveButton;
    private javax.swing.JButton StartButton;
    public javax.swing.JScrollPane TreeScroll;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    // End of variables declaration//GEN-END:variables

}

